package leetcode.editor.cn;

import java.util.*;

public class P350IntersectionOfTwoArraysIi {
    public static void main(String[] args) {
        Solution solution = new P350IntersectionOfTwoArraysIi().new Solution();
        Arrays.stream(solution.intersect(new int[]{61,24,20,58,95,53,17,32,45,85,70,20,83,62,35,89,5,95,12,86,58,77,30,64,46,13,5,92,67,40,20,38,31,18,89,85,7,30,67,34,62,35,47,98,3,41,53,26,66,40,54,44,57,46,70,60,4,63,82,42,65,59,17,98,29,72,1,96,82,66,98,6,92,31,43,81,88,60,10,55,66,82,0,79,11,81}
                , new int[]{5,25,4,39,57,49,93,79,7,8,49,89,2,7,73,88,45,15,34,92,84,38,85,34,16,6,99,0,2,36,68,52,73,50,77,44,61,48})).forEach(e -> System.out.print(e+","));
    }
    
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
        Map<Integer,Integer> hash = new HashMap<>();
        List<Integer> list = new ArrayList<>();
        for (int i : nums1) {
            hash.compute(i, (integer, integer2) -> integer2 == null ? 1 : integer2 + 1);
        }
        for (int i : nums2) {
            Integer compute = hash.compute(i, (integer, integer2) -> integer2 == null ? null : (integer2 < 1 ? null : integer2 - 1));
            if (compute != null) {
                list.add(i);
            }
        }
        int[] result = new int[list.size()];
        int index = 0;
        for (Integer i : list) {
            result[index++] = i;
        }
        return result;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}